package com.edu.neusoft.cookbook.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.neusoft.cookbook.common.ResponseResult;
import com.edu.neusoft.cookbook.common.enums.AppHttpCodeEnum;
import com.edu.neusoft.cookbook.dto.RecipesDTO;
import com.edu.neusoft.cookbook.dto.UserDto;
import com.edu.neusoft.cookbook.entity.Recipes;
import com.edu.neusoft.cookbook.entity.User;
import com.edu.neusoft.cookbook.entity.UserFavorites;
import com.edu.neusoft.cookbook.mapper.RecipesMapper;
import com.edu.neusoft.cookbook.mapper.UserFavoritesMapper;
import com.edu.neusoft.cookbook.mapper.UserMapper;
import com.edu.neusoft.cookbook.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: UserServiceImpl
 * Package: com.atyukaijun.flowers.service.impl
 * Description:
 *
 * @Author 麻了跺跺脚
 * @Create 2024/12/3 9:40
 * @Version 1.0
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserFavoritesMapper userFavoritesMapper;

    @Autowired
    private RecipesMapper recipesMapper;

    /**
     * 用户登录功能
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult login(UserDto dto, HttpSession session) {
        //1.正常登录：用户名和密码
        if (StringUtils.isNotBlank(dto.getUsername()) && StringUtils.isNotBlank(dto.getPassword())) {
            //1.1 根据手机号查询用户信息
            User dbUser = getOne(Wrappers.<User>lambdaQuery().eq(User::getUsername, dto.getUsername()));
            if (dbUser == null) {
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST, "用户信息不存在");
            }

            //1.2 校验密码
            String password = dto.getPassword();
            String dbUserPassword = dbUser.getPassword();
            if (password.equals(dbUserPassword)) {
                session.setAttribute("loginUser", dbUser);
                log.info("session:----------------" + session.getAttribute("loginUser"));
                return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
            } else {
                return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }
        } else {
            //2.用户名或密码为空，登录失败
            return ResponseResult.errorResult(AppHttpCodeEnum.ERROR_USERNAME_PASSWORD_EMPTY);
        }
    }

    @Override
    public List<Recipes> getFavouriteRecipesByUserId(Integer userId) {
        List<UserFavorites> favourites = userFavoritesMapper.selectByUserId(userId);
        List<Integer> recipeIds = favourites.stream()
                .map(UserFavorites::getRecipeId) // 确保这里返回的是 Long
                .collect(Collectors.toList());

        if (recipeIds.isEmpty()) {
            return Collections.emptyList();
        }

        // 从数据库获取 Recipes 实体列表
        List<Recipes> recipes = recipesMapper.selectByRecipeIds(recipeIds);

        // 将 Recipes 实体列表转换为 RecipesDTO 列表
//        List<RecipesDTO> recipesDTOList = recipes.stream()
//                .map(this::convertToRecipesDTO) // 使用方法引用或lambda表达式进行转换
//                .collect(Collectors.toList());
//
//        return recipesDTOList;
        return recipes;
    }

    // 转换方法，将 Recipes 实体转换为 RecipesDTO
    private RecipesDTO convertToRecipesDTO(Recipes recipe) {
        RecipesDTO dto = new RecipesDTO();
        // 设置 DTO 的属性，这些属性应该与 Recipes 实体的属性相对应
        dto.setRecipeId(recipe.getRecipeId());
        dto.setTitle(recipe.getTitle());
        dto.setDescription(recipe.getDescription());
        // ... 设置其他需要的属性
        return dto;
    }

}
